package com.kingyea.app.market.apiInterface.controller;

import com.kingyea.app.market.apiInterface.bean.ApiInterface;
import com.kingyea.app.market.apiInterface.service.ApiInterfaceService;
import com.kingyea.app.market.client.ApimanageServiceClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author
 * @version v1.0.0
 * @description ApiInterface 控制层
 * @date Created in 2018-02-09 17:58:07
 */
@RequestMapping(value = "/apiInterface")
@RestController
public class ApiInterfaceController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ApiInterfaceController.class);

    @Autowired
    private ApiInterfaceService apiInterfaceService;
    @Autowired
    private ApimanageServiceClient apimanageServiceClient;

    /**
     * 查询全部
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Object list(@RequestBody Map<String, Object> api) throws Exception {
        return apiInterfaceService.list(api);
    }

    /**
     * 通过ID查找对象
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ApiInterface detail(@PathVariable String id) throws Exception {
        return apiInterfaceService.getById(id);
    }

    /**
     * 创建
     *
     * @param apiInterface
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ApiInterface create(@RequestBody ApiInterface apiInterface) throws Exception {
        return apiInterfaceService.save(apiInterface);
    }

    /**
     * 更新
     *
     * @param apiInterface
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object update(@RequestBody ApiInterface apiInterface) throws Exception {
        return apiInterfaceService.update(apiInterface);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Object delete(@RequestBody Map<String, Object> api) throws Exception {
        String id = api.get("id").toString();
        int deleteById = 0;
        try {
            deleteById = apiInterfaceService.deleteById(id);
        } catch (Exception e) {
            // TODO: handle exception
        }
        return deleteById;
    }

    @RequestMapping(value = "/searchApimanageService", method = RequestMethod.POST)
    public Object searchApimanageService() {
        Object list = apimanageServiceClient.list();
        return apimanageServiceClient.list();
    }

}
